import { ITimer } from "./ITimer";
import { TimerManager } from "./TimerManager";

export class Timer implements ITimer {
    IsDestory: boolean = false;
    protected action: () => void;
    public Tick(dt:number): boolean {
        this.action();
        return true;
    }

}
export class WaitTimer extends Timer {
    protected leftTriggerTime: number;

    public Set(action: () => void, time: number): void {
        if (time < 0) {
            throw new Error("time must greater equal 0f");
        }
        this.action = action;
        this.leftTriggerTime = time;
    }
    public Tick(dt:number): boolean {
        this.leftTriggerTime -= dt;
        return ((this.leftTriggerTime <= 0) && super.Tick(dt));
    }
}
export class RepeatTimer extends Timer {
    protected leftTriggerTime: number;
    private interval: number;
    public Set(action: () => void, interval: number) {
        this.leftTriggerTime = interval;
        this.interval = interval;
        this.action = action;
    }
    public Tick(dt:number): boolean {
        this.leftTriggerTime -= dt;
        if (this.leftTriggerTime <= 0) {
            super.Tick(dt);
            this.leftTriggerTime = this.interval;
        }
        return false;
    }
}
export class TimerToken {
    public PasueAction: () => void;
    public ResumeAction: () => void;
    public StopAction: () => void;
    public Pasue() {
        if (this.PasueAction)
            this.PasueAction();
    }
    public Resume() {
        if (this.ResumeAction)
            this.ResumeAction();
    }
    public Stop() {
        if (this.StopAction)
            this.StopAction();
    }
}
